/**
 * Created by wangshaohua on 2018/1/25.
 */
moduleContent.controller("AddContentDynamicHtmlForLink", function (UploadFileService, $$CambioConfig, ExerciseContentResource, $scope, $log, $state, $stateParams, GeneralResource, LoadingService, ConstantService, DialogService) {
    initPage($scope);
    $scope.$on("ngDialog.opened", function () {
        dialogOnLoad();
        $scope.typeOptions = Enums.DynamicHtmlTypeEnum.options;
        $scope.currentContentDynamicHtml = {
            parentId: $scope.currentParentId,
            picture: LearningMapTemplateUtils.findExercise('H5').picture
        };
    });

    // ------------------------------------------------------------------------------------------------------

    $scope.cancel = function () {
        DialogService.close();
    };

    $scope.submit = function () {
        $scope.currentContentDynamicHtml.recordType = Enums.ExerciseContentRecordTypeEnum.CONTENT;
        $scope.currentContentDynamicHtml.space = Enums.ExerciseContentSpaceEnum.DYNAMIC_HTML;
        Alert.confirm(function () {
            ExerciseContentResource.saveDynamicHtml({}, $scope.currentContentDynamicHtml, function (result) {
                $scope.$parent.dynamicHtmlId = result.data.value;
                Alert.success();
                DialogService.close();
            });
        })
    }

    // ------------------------------------------------------------------------------------------------------
    // ------------------------------------------------------------------------------------------------------

    $scope.changeDynamicHtmlType = function () {
        $scope.currentContentDynamicHtml.studyClassType = $scope.currentContentDynamicHtml.typeOption.key;
        // switch ($scope.currentContentDynamicHtml.typeOption.key) {
        //     case 'H5':
        //         break;
        //     case 'VIDEO':
        //         break;
        //     case 'AUDIO':
        //         break;
        //     case 'PDF':
        //         break;
        //     default:
        //
        // }
        // console.log(currentContentDynamicHtml.typeOption);
        // $scope.currentContentDynamicHtml.type = type;
        // $log.info($scope.currentContentDynamicHtml.typeOption)
    }

    // ------------------------------------------------------------------------------------------------------
    // ------------------------------------------------------------------------------------------------------
    $scope.uploadPicture = function ($files) {
        if ($files && $files[0]) {
            UploadFileService.uploadFile($files[0], $$CambioConfig.UPLOAD_URL, 'IMG', $scope).then(function (o) {
                $scope.currentContentDynamicHtml.picture = o.id;
            });
        }
    };
});